package com.shop.cloud.common;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.shop.cloud.domain.system.SysResource;

/** 
 * 树形结构构建
 */  
public class TreeNode implements Serializable {  
      
	public static class SysResourceState{
		public boolean selected=false;
		public boolean opened=false;
		public boolean disabled=false;
	}
	
    private static final long serialVersionUID = 1L;  
    /** 
     * 节点ID 
     */  
    public Long id;  
    /** 
     * 节点内容 
     */  
    public String text;  
    /** 
     * 父节点编号 
     */  
    public Long parentId;  
    /** 
     * 节点排序 
     */  
    public int sort;  
    
    public String  attrId;
    
    public String  attrParentId;
    
    
    public SysResourceState state=new SysResourceState();
    /** 
     * 孩子节点列表 
     */  
    public List<TreeNode> children = new ArrayList<TreeNode>();  
      
    /** 
     * 添加子节点 
     *  
     * @param node 子节点 
     */  
    public void addChild(TreeNode node) {  
        children.add(node);  
    } 
    
    public int childLen() {  
        return children.size();
    } 
      
    public void check() {
    	state.selected=true;
	}
    
    /** 
     * 兄弟节点横向排序 
     */  
    public void sortChildren(SysResourceComparatorEnum comparatorEnum) {  
        if ( children.size() != 0 ) {  
            // 对本层节点进行排序（可根据不同的排序属性，传入不同的比较器，这里传入ID比较器）  
            Collections.sort(children, findComparator(comparatorEnum));  
            // 对每个节点的下一层节点进行排序  
            for ( Iterator<TreeNode> it = children.iterator() ; it.hasNext() ; ) {  
                it.next().sortChildren(comparatorEnum);  
            }  
        }  
    }  
      
    /** 
     * 比较器枚举 
     */  
    public static enum SysResourceComparatorEnum {  
        ID, TEXT, SORT  
    }  
      
    /** 
     * 获取比较器 
     */  
    Comparator<TreeNode> findComparator(SysResourceComparatorEnum comparatorEnum) {  
        switch ( comparatorEnum ) {  
            case ID :  
                return new NodeIDComparator();  
            case TEXT :  
                return new NodeTextComparator();  
            case SORT :  
                return new NodeSortComparator();  
            default :  
                return new NodeIDComparator();  
        }  
    }  
      
    /** 
     * 节点ID比较器 
     */  
    class NodeIDComparator implements Comparator<TreeNode> {  
        // 按照节点编号比较  
        public int compare(TreeNode o1, TreeNode o2) {  
            return o1.id.compareTo(o2.id);  
        }  
    }  
      
    /** 
     * 排序编号-节点比较器 
     */  
    class NodeSortComparator implements Comparator<TreeNode> {  
        // 按照节点编号比较  
        public int compare(TreeNode o1, TreeNode o2) {  
            int j1 = o1.sort;  
            int j2 = o2.sort;  
            return (j1 < j2 ? -1 : (j1 == j2 ? 0 : 1));  
        }  
    }  
      
    /** 
     * 父类编号-节点比较器 
     */  
    class NodeTextComparator implements Comparator<TreeNode> {  
        // 按照节点编号比较  
        public int compare(TreeNode o1, TreeNode o2) {  
            return o1.text.compareTo(o2.text);  
        }  
    }  
    
    /** 
     * 说明方法描述：将list转为树tree结构 
     */  
    public static List<SysResource> tree(List<SysResource> allRrecords) {  
        List<SysResource> listParentSysResource = new ArrayList<SysResource>();  
        List<SysResource> listNotParentSysResource = new ArrayList<SysResource>();  
        // 第一步：遍历allRrecords保存所有数据的uuid用于判断是不是根节点  
        Map<Long, Long> mapAllUuid = new HashMap<Long, Long>();  
        Map<Long, SysResource> allSysResourceMap = new HashMap<Long, SysResource>();  
        for (SysResource record : allRrecords) {  
            mapAllUuid.put(record.getId(), record.getId());  
            allSysResourceMap.put(record.getId(), record);  
        }  
        // 第二步：遍历allRrecords找出所有的根节点和非根节点  
        if (allRrecords != null && allRrecords.size() > 0) {  
            for (SysResource record : allRrecords) {  
                if (record.getParentId()!=null  
                    || !mapAllUuid.containsKey(record.getParentId())) {  
                    listParentSysResource.add(record);  
                } else {  
                    listNotParentSysResource.add(record);  
                }  
            }  
        }  
        // 第三步： 递归获取所有子节点  
        if (listParentSysResource.size() > 0) {  
            for (SysResource record : listParentSysResource) {  
                // 添加所有子级  
                record.getResources().addAll(getTreeChildSysResource(listNotParentSysResource, record.getId()));  
            }  
        }  
        return listParentSysResource;  
    }  

    /** 
     * 说明方法描述：递归查询子节点 
     *  
     * @param childList 子节点 
     * @param parentUuid 父节点id 
     */  
    private static List<SysResource> getTreeChildSysResource(List<SysResource> childList, Long parentUuid) {  
        List<SysResource> listParentSysResource = new ArrayList<SysResource>();  
        List<SysResource> listNotParentSysResource = new ArrayList<SysResource>();  
        // 遍历tmpList，找出所有的根节点和非根节点  
        if (childList != null && childList.size() > 0) {  
            for (SysResource record : childList) {  
                // 对比找出父节点  
                if (record.getParentId()== parentUuid) {  
                    listParentSysResource.add(record);  
                } else {  
                    listNotParentSysResource.add(record);  
                }  
            }  
        }  
        // 查询子节点  
        if (listParentSysResource.size() > 0) {  
            for (SysResource record : listParentSysResource) {  
                // 递归查询子节点  
                record.getResources().addAll(getTreeChildSysResource(listNotParentSysResource, record.getId()));  
            }  
        }  
        return listParentSysResource;  
    }  
  
}  